﻿namespace MvcScriptManager
{
    using System.Configuration;
    using System.Security;
    using System.Security.Permissions;
    using System.Web.Configuration;

    /// <summary>
    /// Info about deployment config.
    /// </summary>
    internal sealed class DeploymentSectionCache
    {
        private static readonly DeploymentSectionCache instance = new DeploymentSectionCache();
        private bool? retail;

        /// <summary>
        /// Initializes a new instance of the <see cref="DeploymentSectionCache"/> class.
        /// </summary>
        private DeploymentSectionCache()
        {
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static DeploymentSectionCache Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the app is under retail setting.
        /// </summary>
        /// <value><c>true</c> if under retail; otherwise, <c>false</c>.</value>
        public bool Retail
        {
            get
            {
                if (!this.retail.HasValue)
                {
                    this.retail = new bool?(GetRetailFromConfig());
                }

                return this.retail.Value;
            }
        }

        /// <summary>
        /// Gets the retail setting from config.
        /// </summary>
        /// <returns>The retail setting. <c>true</c> if under retail; otherwise, <c>false</c>.</returns>
        [SecurityCritical, SecurityTreatAsSafe, ConfigurationPermission(SecurityAction.Assert, Unrestricted = true)]
        private static bool GetRetailFromConfig()
        {
            DeploymentSection section = (DeploymentSection)WebConfigurationManager.GetSection("system.web/deployment");
            return section.Retail;
        }
    }
}
